/*
 * HomeHotGoodsHolder.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.viewholder;

import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.ui.widget.listview.ViewHolder;
import com.yidiandao.business.ui.widget.view.FlagView;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.RegexUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 首页热门商品的ViewHolder
 */
public class HomeGoodsHolder extends ViewHolder {

    public ImageView mIvGoodsImage1;
    public TextView mTvGoodsPrice1;
    public LinearLayout mLlytGoods2;
    public ImageView mIvGoodsImage2;
    public TextView mTvGoodsPrice2;
    public ImageView mIvShoppingCart1;
    public ImageView mIvShoppingCart2;
    private TextView mTvGoodsName1;
    private TextView mTvGoodsStock1;
    private TextView mTvGoodsName2;
    private TextView mTvGoodsStock2;
    private TextView mTvSingle1;
    private TextView mTvSingle2;
    private FlagView mFlagView1;
    private FlagView mFlagView2;
    private ImageView mIvGoodsFlag1;
    private ImageView mIvGoodsFlag2;
    private LinearLayout mRootLayout;
    private TextView mTvShopName1;
    private TextView mTvShopName2;
    private TextView mTvSellOut1;
    private TextView mTvSellOut2;

    //    private TextView mTvScore1;

    //    private TextView mTvScore2;

    @Override
    protected void initView(View rootView) {

        mRootLayout = (LinearLayout) rootView.findViewById(R.id.root_layout);

        // 左边商品
        mIvGoodsImage1 = (ImageView) rootView.findViewById(R.id.iv_goods_image_1);
        mTvGoodsName1 = (TextView) rootView.findViewById(R.id.tv_goods_name_1);
        mTvGoodsPrice1 = (TextView) rootView.findViewById(R.id.tv_goods_price_1);
        mTvGoodsStock1 = (TextView) rootView.findViewById(R.id.tv_goods_stock_1);
        mTvSingle1 = (TextView) rootView.findViewById(R.id.tv_single_1);
        mIvShoppingCart1 = (ImageView) rootView.findViewById(R.id.iv_shopping_cart_1);
        mFlagView1 = (FlagView) rootView.findViewById(R.id.flag_view_1);
        mIvGoodsFlag1 = (ImageView) rootView.findViewById(R.id.iv_goods_flag_1);
        mTvShopName1 = (TextView) rootView.findViewById(R.id.tv_shop_name_1);
        mTvSellOut1 = (TextView) rootView.findViewById(R.id.tv_sell_out_1);
        //        mTvScore1 = (TextView) rootView.findViewById(R.id.tv_score_1);
        // 右边商品
        mLlytGoods2 = (LinearLayout) rootView.findViewById(R.id.llyt_goods_2);
        mIvGoodsImage2 = (ImageView) rootView.findViewById(R.id.iv_goods_image_2);
        mTvGoodsName2 = (TextView) rootView.findViewById(R.id.tv_goods_name_2);
        mTvGoodsPrice2 = (TextView) rootView.findViewById(R.id.tv_goods_price_2);
        mTvGoodsStock2 = (TextView) rootView.findViewById(R.id.tv_goods_stock_2);
        mTvSingle2 = (TextView) rootView.findViewById(R.id.tv_single_2);
        mIvShoppingCart2 = (ImageView) rootView.findViewById(R.id.iv_shopping_cart_2);
        mFlagView2 = (FlagView) rootView.findViewById(R.id.flag_view_2);
        mIvGoodsFlag2 = (ImageView) rootView.findViewById(R.id.iv_goods_flag_2);
        mTvShopName2 = (TextView) rootView.findViewById(R.id.tv_shop_name_2);
        mTvSellOut2 = (TextView) rootView.findViewById(R.id.tv_sell_out_2);
        //        mTvScore2 = (TextView) rootView.findViewById(R.id.tv_score_2);

    }

    public void showRootLayoutDivider(boolean isLast) {
        if (isLast) {
            mRootLayout.setPadding(0, 0, 0, 0);
        } else {
            mRootLayout.setPadding(0, 0, 0, UIUtils.dp2px(5));
        }
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_home_goods_grid;
    }

    public void refreshUI1(Goods goods1) {
        ImageLoader.loadImageWithPlaceHolder(mContext, mIvGoodsImage1, goods1.getGoodsThumb());
        mTvGoodsName1.setText(goods1.getGoodsName());
        mFlagView1.setFlagText(goods1.getFavourableNotice(), goods1.getFavourableType());
        int goodsFlagResId1 = goods1.getGoodsFlagResId();
        if (goodsFlagResId1 != -1) {
            mIvGoodsFlag1.setVisibility(View.VISIBLE);
            mIvGoodsFlag1.setImageResource(goodsFlagResId1);
        } else {
            mIvGoodsFlag1.setVisibility(View.INVISIBLE);
        }
        mTvShopName1.setText(goods1.getSupplierName());
        //        mTvScore1.setText(UIUtils.getString(R.string.score, "100"));
        String goodsNumberDesc1 = goods1.getGoodsNumberDesc();

        boolean isNumber1 = RegexUtils.isFloat(goodsNumberDesc1);

        mTvGoodsStock1.setText(isNumber1 ? UIUtils.getString(R.string.stock_hint_unit, goodsNumberDesc1, goods1.getGoodsUnit())
                : UIUtils.getString(R.string.stock_hint_unit_1, goodsNumberDesc1));

        // 单瓶价
        String singlePrice1 = goods1.getAvgShopPriceFloat();
        if (RegexUtils.isFloat(singlePrice1) && Float.parseFloat(singlePrice1) == 0) {
            // 单瓶价等于0，不显示单瓶价格
            mTvSingle1.setVisibility(View.INVISIBLE);
        } else {
            // 显示单瓶价格
            // 显示库存layout
            mTvSingle1.setVisibility(View.VISIBLE);
            // 库存layout显示库存的内容
            mTvSingle1.setText(getSingePrice(singlePrice1));
        }
        mTvGoodsPrice1.setText(TextRenderUtils.getBeautyPrice(goods1.getShopPriceFloat(), goods1.getGoodsUnit(), UIUtils.dp2px(18), UIUtils.dp2px(14)));

        int stockNumber1 = goods1.getGoodsNumber();
        mTvSellOut1.setVisibility(stockNumber1 > 0 ? View.GONE : View.VISIBLE);
        mIvShoppingCart1.setVisibility(stockNumber1 > 0 ? View.VISIBLE : View.GONE);
    }

    private SpannableStringBuilder getSingePrice(String singlePrice) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        SpannableString pre = TextRenderUtils.getAbsoluteSizeText(UIUtils.getString(R.string.single_hint), UIUtils.dp2px(13));
        SpannableString suf = TextRenderUtils.getBeautyPrice(singlePrice, null, UIUtils.dp2px(15), UIUtils.dp2px(12));

        builder.append(pre);
        builder.append(suf);

        return builder;
    }

    public void refreshUI2(Goods goods2) {
        ImageLoader.loadImageWithPlaceHolder(mContext, mIvGoodsImage2, goods2.getGoodsThumb());
        mTvGoodsName2.setText(goods2.getGoodsName());
        mFlagView2.setFlagText(goods2.getFavourableNotice(), goods2.getFavourableType());
        int goodsFlagResId2 = goods2.getGoodsFlagResId();
        if (goodsFlagResId2 != -1) {
            mIvGoodsFlag2.setVisibility(View.VISIBLE);
            mIvGoodsFlag2.setImageResource(goodsFlagResId2);
        } else {
            mIvGoodsFlag2.setVisibility(View.INVISIBLE);
        }
        mTvShopName2.setText(goods2.getSupplierName());
        // todo:加入积分
        //        mTvScore2.setText(UIUtils.getString(R.string.score, "100"));
        String goodsNumberDesc2 = goods2.getGoodsNumberDesc();

        boolean isNumber2 = RegexUtils.isFloat(goodsNumberDesc2);
        // 单瓶价
        String singlePrice2 = goods2.getAvgShopPriceFloat();

        mTvGoodsStock2.setText(isNumber2 ? UIUtils.getString(R.string.stock_hint_unit, goodsNumberDesc2, goods2.getGoodsUnit())
                : UIUtils.getString(R.string.stock_hint_unit_1, goodsNumberDesc2));

        if (RegexUtils.isFloat(singlePrice2) && Float.parseFloat(singlePrice2) == 0) {
            // 单瓶价等于0，不显示单瓶价格
            mTvSingle2.setVisibility(View.INVISIBLE);
        } else {
            // 显示单瓶价格
            // 显示库存layout
            mTvSingle2.setVisibility(View.VISIBLE);
            // 库存layout显示库存的内容
            mTvSingle2.setText(getSingePrice(singlePrice2));
        }
        mTvGoodsPrice2.setText(TextRenderUtils.getBeautyPrice(goods2.getShopPriceFloat(), goods2.getGoodsUnit(), UIUtils.dp2px(18), UIUtils.dp2px(14)));

        int stockNumber2 = goods2.getGoodsNumber();
        mTvSellOut2.setVisibility(stockNumber2 > 0 ? View.GONE : View.VISIBLE);
        mIvShoppingCart2.setVisibility(stockNumber2 > 0 ? View.VISIBLE : View.GONE);
    }
}
